package com.xzk.service.impl;

import com.xzk.bean.Courier;
import com.xzk.dao.CourierDao;
import com.xzk.exception.DuplicateIdCardException;
import com.xzk.exception.DuplicatePhoneException;
import com.xzk.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourierServiceImpl
 * @Description 快递员Service层
 * @Author 0715-YuHao
 * @Date 2020/10/7 16:09
 * @Version 1.0
 */
@Service("courierService")
public class CourierServiceImpl implements CourierService {

    @Autowired
    private CourierDao dao;

    @Override
    public Map<String, Integer> console() {
        return dao.console();
    }

    @Override
    public List<Courier> findAll() {
        return dao.findAll();
    }

    @Override
    public void updateLoginTime(String userPhone) {
        dao.updateLoginTime(userPhone);
    }

    @Override
    public Courier findByPhone(String phone) {
        return dao.findByPhone(phone);
    }

    @Override
    public boolean insert(Courier courier) throws DuplicateIdCardException, DuplicatePhoneException {
        try {
            return dao.insert(courier) > 0;
        }catch (Exception e) {
            if (e.getMessage().endsWith("for key 'idCard'")) {
                throw new DuplicateIdCardException(e.getMessage());
            }else if (e.getMessage().endsWith("for key 'phone'")) {
                throw new DuplicatePhoneException(e.getMessage());
            }else {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean numberDispatchAddOne(String userPhone) {
        return dao.numberDispatchAddOne(userPhone) > 0;
    }

    @Override
    public boolean update(int id, Courier c) throws DuplicateIdCardException, DuplicatePhoneException {
        Map map = new HashMap();
        map.put("id", id);
        map.put("name", c.getName());
        map.put("phone", c.getPhone());
        map.put("idCard", c.getIdCard());
        map.put("password", c.getPassword());
        try {
            return dao.update(map) > 0;
        }catch (Exception e) {
            if (e.getMessage().endsWith("for key 'idCard'")) {
                throw new DuplicateIdCardException(e.getMessage());
            }else if (e.getMessage().endsWith("for key 'phone'")) {
                throw new DuplicatePhoneException(e.getMessage());
            }else {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean updateByWx(String userPhone, Courier c){
        Map map = new HashMap();
        map.put("name", c.getName());
        map.put("phone", userPhone);
        map.put("password", c.getPassword());
        return dao.updateByWx(map) > 0;
    }

    @Override
    public boolean delete(int id, String phone, String idCard) {
        Map map = new HashMap();
        map.put("id", id);
        map.put("phone", "#" + phone);
        map.put("idCard", "#" +idCard);
        return dao.delete(map) > 0;
    }
}
